﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AGrandVenture
{
    public static class MessageWindow
    {
        //Message Window to display updates to the player.  Located at bottom left of screen and message will scroll upwards and slowly fade out

        private static SpriteFont font; // font to use
        private static Vector2 origin; //bottomleft corner of window
        private static float timeToDisplay; //length of time that message will be displayed before fading out.
        private static int offset; // height of messageline;
        private static int numberOfLines = 8; // controls how many lines to display at once
        private static Color fontColour; // font Colour
        private static Color defaultFontColour; //default font colour

        public static List<Message> MessageBuffer = new List<Message>(); // Holds messages to be displayed
        
        public static void Initialise(SpriteFont spriteFont, Vector2 location, float time, int lineHeight, Color defaultColour)
        {
            MessageBuffer.Clear();

            font = spriteFont;
            origin = location;
            timeToDisplay = time;
            offset = lineHeight;
            defaultFontColour = defaultColour;

        }

        public static void AddMessage(string message)
        {
            fontColour = defaultFontColour;
            Message messageToDisplay = new Message(message, fontColour);
            messageToDisplay.aliveTimer = timeToDisplay;
            MessageBuffer.Add(messageToDisplay);

            if (MessageBuffer.Count > numberOfLines) // If message bugger is full, removes the oldest item from MessageBuffer
            {
                MessageBuffer.RemoveAt(0);
            }
        }

        public static void AddMessage(string message, Color colour)
        {
            fontColour = colour;
            Message messageToDisplay = new Message(message, fontColour);
            messageToDisplay.aliveTimer = timeToDisplay;
            MessageBuffer.Add(messageToDisplay);

            if (MessageBuffer.Count > numberOfLines) // If message bugger is full, removes the oldest item from MessageBuffer
            {
                MessageBuffer.RemoveAt(0);
            }
        }

        public static void RemoveMessage(int index)
        {
            MessageBuffer.RemoveAt(index);
        }

        public static void Update(GameTime gameTime)
        {

            for (int x = MessageBuffer.Count - 1; x >= 0; x--)
            {
                MessageBuffer[x].Update(gameTime);
                if (!MessageBuffer[x].active)
                {
                    MessageBuffer.RemoveAt(x);
                }
            }

            TextZoomManager.Update(); // Manages text zoom effect above sprites
           


        }

        public static void Draw(SpriteBatch spriteBatch)
        {

            for (int x = 0 ; x < MessageBuffer.Count; x++)
            {
                spriteBatch.DrawString(font, MessageBuffer[x].message, new Vector2(origin.X, origin.Y - ((MessageBuffer.Count - x) * offset)), MessageBuffer[x].colour);
            }

            TextZoomManager.Draw(spriteBatch); // Draws text zoom effect above sprites
        }

    }
}
